﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraTab;
using DevExpress.XtraBars.Ribbon;
using DoAn_QuanLyCafe.App_Code.BLL;
using DoAn_QuanLyCafe.App_Code.DAL;
using DoAn_QuanLyCafe.QLNhan_vien;


namespace DoAn_QuanLyCafe
{
    public partial class frmQuanLyCafe : DevExpress.XtraEditors.XtraForm
    {
        private RibbonPage oldPage = null;
        BanBLL BB = new BanBLL();
        GiaithuongBLL GTB = new GiaithuongBLL();
     
        public frmQuanLyCafe()
        {
            InitializeComponent();
            Nhanvien nv = new Nhanvien();
            dataGridViewNhanvien.DataSource = nv.ShowData();
        }

        private void QuanLyCafe_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cafe_DS.PHIEUDUTHUONG' table. You can move, or remove it, as needed.
            this.pHIEUDUTHUONGTableAdapter.Fill(this.cafe_DS.PHIEUDUTHUONG);
            // TODO: This line of code loads data into the 'cafe_DS.GIAITHUONG' table. You can move, or remove it, as needed.
            this.gIAITHUONGTableAdapter.Fill(this.cafe_DS.GIAITHUONG);
            // TODO: This line of code loads data into the 'cafe_DS.BAN' table. You can move, or remove it, as needed.
            this.bANTableAdapter.Fill(this.cafe_DS.BAN);
            menuSkin(); 
            initTab();
            oldPage = ribbonPageNhanVien;
            ribbonController.SelectedPage = oldPage;
            
        }

        #region Load Control

        // tạo skin cho form
        private void initTab()
        {
            //xtraTabPageNV.PageVisible = true;
            //xtraTabPageQLBan.PageVisible = false;
            //xtraTabPagePGM_M.PageVisible = false;
            //xtraTabPagePDT_GT.PageVisible = false;
            //xtraTabPageSaoLuu.PageVisible = false;
            //xtraTabPageBaoCao_BC.PageVisible = false;
            //xtraTabPageMonAn.PageVisible = false;
            //xtraTabPageLichLV.PageVisible = false;
            //AddTableToTabPage();
        }
        // load tab khi show form
        private void menuSkin()
        {
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            SkinHelper.InitSkinPopupMenu(barSubLink);
        }
        // khi đổi link tab page
        private void LoadTabPage(XtraTabPage oldTab, XtraTabPage newTab)
        {
            if (oldTab != null && newTab != null)
            {
                oldTab.PageVisible = false;
                newTab.PageVisible = true;
            }
        }

        private void AddTableToTabPage()
        {
            SimpleButton btn= null;
            xtraTabPageQLBan.SuspendLayout();
            int soBan = 21;
            int x = 0, y = 0;
            int i = 0;
            for (int j = 0; j < soBan; j++)
            {
                if (j == i)
                {
                    i = j + 5;
                    x = 1; y += 1;
                }
                btn = new SimpleButton();
                btn.Text = "Bàn " + (j+1);
                btn.Location = new Point(110*(x++), 80*y);
                btn.Size = new System.Drawing.Size(100, 75);
                btn.Tag= (j+1);
                btn.ToolTip = "Trống";
                btn.Click += new EventHandler(this.btn_Click);
                xtraScrollableControlBan.Controls.Add(btn);
            }
            xtraScrollableControlBan.Select();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            SimpleButton btn = (SimpleButton)sender;
            frmBan b = new frmBan(this.LookAndFeel, int.Parse(btn.Tag.ToString()));
            b.ShowDialog();
        }

        #endregion

        #region navBar

        private void navBarItemBan_Trangthai_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPageQLBan);
        }

        private void navBarItemPGM_Mon_DanhSach_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPagePGM_M);
        }

        private void navBarItemPDT_GT_DanhSach_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPagePDT_GT);
        }

        private void navBarItemBK_RS_Backup_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPageSaoLuu);
        }

        private void navBarItemQLLichLV_LichLV_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPageLichLV);
        }

        private void navBarItemBaoCao_XemBC_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            LoadTabPage(oldTab, xtraTabPageBaoCao_BC);
        }

        #endregion

        private void barButtonTrogiup_ItemClick(object sender, ItemClickEventArgs e)
        {
            MessageBox.Show("Đang nâng cấp chức năng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void frmQuanLyCafe_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!(sender is BarManager))
            {
                DialogResult r = MessageBox.Show("Bạn có muốn thoát khỏi chương trình ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    e.Cancel = false;
                    Application.ExitThread();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void ribbonController_Click(object sender, EventArgs e)
        {
            //RibbonPage newPage = ribbonController.SelectedPage;
            
            //if (newPage != oldPage)
            //{
            //    oldPage = newPage;
            //    if (newPage == ribbonPageNhanVien)
            //    {
            //        XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            //        LoadTabPage(oldTab, xtraTabPageNV);
            //    }
            //    else
            //    {
            //        if (newPage == ribbonPageMonAn || newPage == ribbonPageDoUong)
            //        {
            //            XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            //            LoadTabPage(oldTab, xtraTabPageMonAn);
            //        }
            //        else
            //        {
            //            if (newPage == ribbonPageSaoLuu)
            //            {
            //                XtraTabPage oldTab = xtraTabController.SelectedTabPage;
            //                LoadTabPage(oldTab, xtraTabPageSaoLuu);
            //            }
            //        }
            //    }
            //}

        }


        private void txtSaoLuu_timkiem_Enter(object sender, EventArgs e)
        {
            txtSaoLuu_timkiem.Text = "";
        }

        private void txtSaoLuu_timkiem_Leave(object sender, EventArgs e)
        {

        }

        private void btnThucAn_DoUong_Load_Click(object sender, EventArgs e)
        {
            ThucAn_DoUong_ds ds = new ThucAn_DoUong_ds();
            DataTable tbl = ds.getDanhsach();
            dgvMonAn_ThucUong.DataSource = tbl;
        }

        private void btnSaoLuu_Update_Click(object sender, EventArgs e)
        {
            DataTable tbl =(DataTable) dgvMonAn_ThucUong.DataSource;
            ThucAn_DoUong_ds ds = new ThucAn_DoUong_ds();
            ds.Update(tbl);
            MessageBox.Show("Cập nhật thành công", "Thông báo cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = gridViewTA_DU.FocusedRowHandle;
            gridViewTA_DU.DeleteRow(i);
        }

    

        private void dataGripView_NhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
            
        }

        private void btnNhanvien_taomoi_Click(object sender, EventArgs e)
        {
            txtNhanvien_Diachi.Text = null;
            txtNhanvien_Dienthoai.Text = null;
            txtNhanvien_Hoten.Text = null;
            txtNhanvien_maNV.Text = null;
            txtNhanvien_ngaysinh.Text = null;
            
        }

  
        private void FormNhanvien_Paint(object sender, PaintEventArgs e)
        {
            //Nhanvien nv = new Nhanvien();
            //dataGridViewNhanvien.DataSource = nv.ShowData();
        }

        private void dataGridViewNhanvien_Click(object sender, EventArgs e)
        {
            #region Dat textbox bang gia tri tuong ung trong datagripview

            int dong = dataGridViewNhanvien.CurrentRow.Index;
            if (dong >= 0)
            {
                try
                {
                    txtNhanvien_maNV.Text = dataGridViewNhanvien.Rows[dong].Cells[0].Value.ToString().Trim();
                    txtNhanvien_Hoten.Text = dataGridViewNhanvien.Rows[dong].Cells[1].Value.ToString();
                    txtNhanvien_Diachi.Text = dataGridViewNhanvien.Rows[dong].Cells[3].Value.ToString();
                    txtNhanvien_Dienthoai.Text = dataGridViewNhanvien.Rows[dong].Cells[4].Value.ToString().Trim();

                    string temp_date = dataGridViewNhanvien.Rows[dong].Cells[2].Value.ToString();
                    DateTimeOffset dos = DateTimeOffset.Parse(temp_date);
                    DateTime date = new DateTime(dos.Year, dos.Month, dos.Day, dos.Hour, dos.Minute, dos.Second);
                    txtNhanvien_ngaysinh.Value = date;
                }
                catch { }
            }
            #endregion
        }

        public void load()
        {
            Nhanvien nv = new Nhanvien();
            dataGridViewNhanvien.DataSource = nv.ShowData();
        }
        // Su kiem Them Nhan vien
        private void btnNhanvien_Them_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien();
            nv.Insert(txtNhanvien_Hoten.Text.ToString(),txtNhanvien_ngaysinh.Text.ToString(),txtNhanvien_Diachi.Text.ToString(),txtNhanvien_Dienthoai.Text.ToString());
            dataGridViewNhanvien.DataSource = nv.ShowData();
        }

        private void btnNhanvien_xoa_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien();
            int manv = int.Parse(txtNhanvien_maNV.Text.ToString().Trim());
            nv.Delete(manv);
            dataGridViewNhanvien.DataSource = nv.ShowData();
        }

        private void btnNhanvien_Capnhap_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien();

            nv.Update(int.Parse(txtNhanvien_maNV.Text.Trim()), txtNhanvien_Hoten.Text.Trim(), txtNhanvien_ngaysinh.Text.Trim(), txtNhanvien_Diachi.Text.Trim(), txtNhanvien_Dienthoai.Text.Trim());
            dataGridViewNhanvien.DataSource = nv.ShowData();
        }

        private void barButtonNV_DoiMatKhau_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmNhanvien_Doipass fr = new frmNhanvien_Doipass();
            fr.Show();
        }
<<<<<<< .mine
=======

        private void barButtonNV_XemTTCN_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmThongtincanhan fr = new frmThongtincanhan();
            fr.Show();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDangNhap fr = new frmDangNhap();
            fr.Show();
            this.Visible = false;
        }

        private void btnNhanvien_QuanlyTK_ItemClick(object sender, ItemClickEventArgs e)
        {
            frameQuanlyTaikhoan fr = new frameQuanlyTaikhoan();
            fr.Show();
        }
        //
      
>>>>>>> .r25

        private void barButtonNV_XemTTCN_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmTT_Nhanvien fr = new frmTT_Nhanvien();
            fr.Show();
        }
        // tab BAN

        private void btnBan_Them_Click(object sender, EventArgs e)
        {

            BB.Insert("", txtBan_succhua.Text);
            GridViewBan.DataSource = BB.ShowData();
        }

        private void btnBan_taomoi_Click(object sender, EventArgs e)
        {
            txtBan_stt.Text = null;
            txtBan_succhua.Text = null;
        }

        private void btnBan_del_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn muốn xóa", "Thông báo xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            try
            {
                BB.Delete(Int32.Parse(txtBan_stt.Text.ToString().Trim()));
                GridViewBan.DataSource = BB.ShowData();
            }
            catch { }

        }

        private void btnBan_refresh_Click(object sender, EventArgs e)
        {
           
            GridViewBan.DataSource = BB.ShowData();
        }

        private void btnBan_update_Click(object sender, EventArgs e)
        {
            BB.Update(txtBan_stt.Text, txtBan_succhua.Text);
            GridViewBan.DataSource = BB.ShowData();
            MessageBox.Show("Cập nhật thành công", "Thông báo cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

        }

        int rowBan;
        private void GridViewBan_Click_1(object sender, EventArgs e)
        {
            rowBan = GridViewBan.CurrentRow.Index;
            if (rowBan >= 0)
                try
                {
                    txtBan_stt.Text = GridViewBan.Rows[rowBan].Cells[0].Value.ToString();
                    txtBan_succhua.Text = GridViewBan.Rows[rowBan].Cells[1].Value.ToString();
                }
                catch { };
        }

        // tab giai thuong
        private void btnGiaithuong_add_Click(object sender, EventArgs e)
        {
            
            GTB.Insert("",txtGiaithuong_ten.Text,txtGiaithuong_trigia.Text);
            GridViewGiaithuong.DataSource = GTB.Showdata();
        }
        private void btnGiaithuong_taomoi_Click(object sender, EventArgs e)
        {
            txtGiaithuong_ma.Text = null;
            txtGiaithuong_ten.Text = null;
            txtGiaithuong_trigia.Text = null;
        }
        private void btnGiaithuong_del_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn muốn xóa", "Thông báo xóa",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
            try
            {
                GTB.Delete(Int32.Parse(txtGiaithuong_ma.Text.ToString().Trim()));
                GridViewGiaithuong.DataSource = GTB.Showdata();
            }
            catch { }
        }

        private void btnGiaithuong_refresh_Click(object sender, EventArgs e)
        {
            GridViewGiaithuong.DataSource = GTB.Showdata();
        }

        private void btnGiaithuong_update_Click(object sender, EventArgs e)
        {
            GTB.Update(txtGiaithuong_ma.Text, txtGiaithuong_ten.Text, txtGiaithuong_trigia.Text);
            GridViewGiaithuong.DataSource = GTB.Showdata();
            MessageBox.Show("Cập nhật thành công", "Thông báo cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        int rowGiaithuong;
        private void GridViewGiaithuong_Click_1(object sender, EventArgs e)
        {
            rowGiaithuong = GridViewGiaithuong.CurrentRow.Index;
            if (rowGiaithuong >= 0)
                try
                {
                    txtGiaithuong_ma.Text = GridViewGiaithuong.Rows[rowGiaithuong].Cells[0].Value.ToString().Trim();
                    txtGiaithuong_ten.Text = GridViewGiaithuong.Rows[rowGiaithuong].Cells[1].Value.ToString().Trim();
                    txtGiaithuong_trigia.Text = GridViewGiaithuong.Rows[rowGiaithuong].Cells[2].Value.ToString().Trim();
                }
                catch { }
        }



    }
}